home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / _read.c < prev    next >
C/C++ Source or Header  |  1998-12-25  |  2KB  |  106 lines

  1. RCS_ID_C="$Id: _read.c,v 4.1 1994/09/29 23:09:02 jraja Exp $";
  2. /*
  3.  *      _read.c - read() for both files and sockets (SAS/C)
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <ios1.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <dos/dos.h>
  17. #include <proto/dos.h>
  18.  
  19. #include <bsdsocket.h>
  20. #include "libcheck.h"
  21.  
  22. //extern int __io2errno(long);
  23.  
  24. int
  25. __read(int fd, void *buffer, unsigned int length)
  26. {
  27.   struct UFB *ufb;
  28.   int         count;
  29.   char        ch, *ptr, *nextptr, *endptr;
  30.  
  31.   /*
  32.    * Check for the break signals
  33.    */
  34.   __chkabort();
  35.   /*
  36.    * find the ufb *
  37.    */
  38.   if ((ufb = __chkufb(fd)) == NULL) {
  39.     errno = EINVAL;
  40.     return -1;
  41.   }
  42.   /*
  43.    * Check if read is allowed
  44.    */
  45.   if (!(ufb->ufbflg & UFB_RA)) {
  46.     _OSERR = ERROR_READ_PROTECTED;
  47.     errno = EIO;
  48.     return -1;
  49.   }
  50.  
  51.   if (ufb->ufbflg & UFB_SOCK)
  52.     if(!checksocketlib()) return -1;
  53.  
  54.   /*
  55.    * Do the Actual read
  56.    */
  57.   _OSERR = 0;
  58.   if (ufb->ufbflg & UFB_SOCK) {
  59.     if ((count = recv(fd, (UBYTE *)buffer, length, 0)) < 0) {
  60.       return -1;
  61.     }
  62.   }
  63.   else {
  64.     if ((count = Read(ufb->ufbfh, buffer, length)) == -1) {
  65.       errno = __io2errno(_OSERR = IoErr());
  66.       return -1;
  67.     }
  68.   }
  69.   /*
  70.    * Check if translation is not needed
  71.    */
  72.   if (count == 0 || !(ufb->ufbflg & UFB_XLAT))
  73.     return count;
  74.   
  75.   endptr = (char *)buffer + count;    /* first point NOT in buffer */
  76.   
  77.   if (endptr[-1] == 0x0D)   /* checks last char */
  78. #if 0
  79.     Read(ufb->ufbfh, buffer, 1); /* overwrites first read byte ! */
  80.   /*            BUG  ^^^^^^ */
  81. #else
  82.     count--, endptr--;
  83. #endif
  84.  
  85.   /*
  86.    * Remove 0x0D's (CR) (This doesn't remove a CR at the end of the buffer).
  87.    */
  88.   if ((ptr = memchr(buffer, 0x0D, count)) != NULL) {
  89.     nextptr = ptr + 1;
  90.     while (nextptr < endptr) {
  91.       if ((ch = *nextptr) != 0x0D)
  92.     *ptr++ = ch;
  93.       nextptr++;
  94.     }
  95.     count = ptr - (char *)buffer;
  96.   }
  97.  
  98.   /*
  99.    * Test for CTRL-Z (end of file)
  100.    */
  101.   if ((ptr = memchr(buffer, 0x1A, count)) == NULL)
  102.     return count;
  103.  
  104.   return ptr - (char *)buffer;
  105. }
  106.